package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdkj.mapper.MemberMapper;
import com.sdkj.mapper.OrdersMapper;
import com.sdkj.pojo.Member;
import com.sdkj.service.ReportService;
import com.sdkj.util.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class ReportServiceImpl implements ReportService{
    @Resource
    MemberMapper memberMapper;
    @Resource
    OrdersMapper ordersMapper;
    //统计这个月截至 1(12个月)年的会员数量信息
    @Override
    public Map<String, Object> getMemberReport() {
        //存放月份和数量综合数据
        Map<String,Object> map = new HashMap<>();
        //存放月份数据
        List<String> months =  new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM");
        //java提供的日历工具类
        Calendar calendar = Calendar.getInstance();
        //当前日期 后退12个月
        calendar.add(Calendar.MONTH,-12);
        //calendar  2023-10-23
        //日历工具类 完成12次数循环
        for (int i = 0; i < 12 ; i++) {
            calendar.add(Calendar.MONTH,1);
            Date time = calendar.getTime();
            String format = simpleDateFormat.format(time); // "2023.11"
            months.add(format);
        }
        List<Long> memberCount = new ArrayList<>();
        //统计每个月份 1号 到 31号 会员数量
        for (String month : months) {
            String str = month.replace(".", "-"); // 2023-11
            String startTime = str + "-01";  // "2023-11-01"
            String endTime = str + "-31";    // "2023-11-31"
            LambdaQueryWrapper<Member> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.between(Member::getRegtime,startTime,endTime);
            Long count = memberMapper.selectCount(lambdaQueryWrapper);
            memberCount.add(count);
        }
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }
    //统计每个被预约的套餐占比
    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String,Object> map = new HashMap<>();
        //套餐的名称
        List<Object> names = new ArrayList<>();
        //套餐的名字 和被预约次数
        List<Map<String, Object>> setmealNameAndCount = ordersMapper.getSetmealNameAndCount();
        for (Map<String, Object> stringObjectMap : setmealNameAndCount) {
            Object name =  map.get("name");
            names.add(name);
        }
        map.put("setmealNames",names);
        map.put("setmealCount",setmealNameAndCount);
        return map;
    }
    //获取运行数据
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String,Object> map = new HashMap<>();
        try {
            //获取今日的日期
            Date today = new Date();
            String todayStr = DateUtils.parseDate2String(today,"yyyy-MM-dd");
            //获取周的第一天日期
            Date firstDayOfWeek = DateUtils.getFirstDayOfWeek(today);
            String firstDayOfWeekStr = DateUtils.parseDate2String(firstDayOfWeek);

            //获取该月的第一天
            Date firstDay4ThisMonth = DateUtils.getFirstDay4ThisMonth();
            String firstDay4ThisMonthStr = DateUtils.parseDate2String(firstDay4ThisMonth);
            map.put("reportDate", todayStr);

            //获取今日新增会员数
            LambdaQueryWrapper<Member> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Member::getRegtime, todayStr);
            Long todayNewMember = memberMapper.selectCount(lambdaQueryWrapper);
            map.put("todayNewMember", todayNewMember);

            //获取总的会员数量
            Integer totalMember = memberMapper.getMemberCount();
            map.put("totalMember",totalMember);

            //本周新增会员数据
            Integer thisWeekNewMember = memberMapper.getMemberCountOfWeek(firstDayOfWeekStr, todayStr);
            map.put("thisWeekNewMember",thisWeekNewMember);
            //本月新增会员数据
            Integer thisMonthNewMember = memberMapper.getMemberCountOfWeek(firstDay4ThisMonthStr, todayStr);
            map.put("thisMonthNewMember",thisMonthNewMember);

            //今日预约人数
            Integer todayOrderNumber = ordersMapper.getTodayOrderInfo(todayStr);
            map.put("todayOrderNumber",todayOrderNumber);


            //今日到诊人数
            Integer todayVisitsNumber = ordersMapper.getTodayVisitsNumber(todayStr);
            map.put("todayVisitsNumber",todayVisitsNumber);


            //本周预约数
            Integer thisWeekOrderNumber = ordersMapper.getOrderInfo(firstDayOfWeekStr, todayStr);
            map.put("thisWeekOrderNumber",thisWeekOrderNumber);

            //本周到诊数
            Integer thisWeekVisitsNumber = ordersMapper.getVisitsNumber(firstDayOfWeekStr, todayStr);
            map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);


            //本月预约数
            Integer thisMonthOrderNumber = ordersMapper.getOrderInfo(firstDay4ThisMonthStr, todayStr);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);

            //本月到诊数
            Integer thisMonthVisitsNumber = ordersMapper.getVisitsNumber(firstDay4ThisMonthStr, todayStr);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

            List<Map<String, Object>> hotSetmeal = ordersMapper.getHotSetmeal();

            map.put("hotSetmeal",hotSetmeal);

        }catch (Exception ex){
            ex.printStackTrace();
        }
        return map;
    }



}
